package com.gxa.ehome.controller;

import com.gxa.ehome.pojo.SysUser;
import com.gxa.ehome.service.SysUserService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
@RequestMapping("/index")
public class IndexController {

    // 响应前端请求,没有任何页面可以展示
    // 请求hello响应的过程中, 获取web程序中的对象
    // HttpServlet/HttpServletRequest/HttpServletResponse/ServletContext/ServletConfig/Session/Cookie
    @RequestMapping("/hello")
    @ResponseBody
    public void hello(HttpServletRequest request){
        System.out.println("有前端发起了对 /index/hello 的请求");
        System.out.println("request:"+request);

        //获取当前Servlet(DispatcherServlet)的上下文对象
        ServletContext servletContext = request.getServletContext();
        System.out.println("servletContext:"+servletContext);

        // 通过上下文对象,获取目标资源的本地路径
        // /表示当前运行的java程序所在本地目录
        System.out.println(servletContext.getRealPath("/"));
    }

    // 访问jsp页面, springmvc的返回值就是在转发
    // 将数据带入jsp : request.setAttribute()
    // ModelMap: 存放数据的数据模型
    // ModelAndView: 包含数据和页面信息整合对象
    //@ModelAttribute: 通过name/value指定数据的名称,可以把方法中定义好的对象传递
    @RequestMapping("/index")
    public String index(ModelMap modelMap){
        modelMap.addAttribute("username","admin");
      //  return "/WEB-INF/jsp/index.jsp";
        // 当配置了前后缀之后:
        return "index";
    }

    @RequestMapping("/index2")
    public ModelAndView index(ModelAndView modelAndView){
        modelAndView.addObject("key","value");
        modelAndView.setViewName("index");  // "/WEB-INF/jsp/index.jsp";
        return modelAndView;   //显然该操作比直接返回String被解析为jsp地址繁琐一些
    }

    //利用produces(设定响应方式 , 来解决直接使用@ResponseBody给前端传递中文字符串
    // application/json;charset=utf-8)
    // 几乎没有应用场景
    @RequestMapping(value = "/string",produces = {"application/json;charset=utf-8"})
    @ResponseBody
    public String getString(){
        return "你好,来啦";
    }

    //当我们要给前端返回对象,实际上是对对象做json格式的转换处理
    // 使用fastjson等json工具做序列化处理: {jobNumber:"GL001",name:"zhangSan"}
    // 否则会抛出:No converter found
    // 解决方案: fastjson(不推荐) / jackson(另一种json格式处理,推荐)
    // 只需要导入jackson的jar包
    @RequestMapping("/sysUser")
    @ResponseBody
    public SysUser getSysUser(){
        SysUser sysUser = new SysUser();
        sysUser.setName("aaa");
        sysUser.setJobNumber("GL001");
        return sysUser;
    }

    @Resource
    private SysUserService sysUserService;

    @RequestMapping("/list")
    @ResponseBody
    public List<SysUser> getList(@RequestHeader("token") String token){
        System.out.println("token:"+token);

        List<SysUser> sysUsers = sysUserService.selectAll();
        return sysUsers;
    }




}
